import {Component,Input} from '@angular/core';
import {AlertController, IonicPage, NavController, NavParams, ToastController, Platform, ItemSliding} from 'ionic-angular';
import {FileTransfer, } from '@ionic-native/file-transfer';

import {FileService} from '../../../services/native/file/file.service';
import {RecorderService} from "../../../services/recorder.service";
import {LocalStorageService} from "../../../services/native/local-storage/local-storage.service";
import {ToastService} from '../../../services/common/toast.service';

@Component({
    selector: 'adp-voice-player',
    templateUrl: 'adp-voice-player.html',
    providers:[
        FileTransfer,
        FileService,
        RecorderService,
        LocalStorageService,
        ToastService
    ]
})
export class AdpVoicePlayerComponent {
    // 语音地址
    @Input() voiceUrl:string;
    // 语音时长
    @Input() voiceLength:number;
    // 语音Id
    @Input() id:string;
    // 是否是新语音
    @Input() isNewVoice:boolean;
   /* //当前语音ID
    @Input() currentVoiceId:string;
    // 当前播放语音ID
    @Input() voiceId:string;*/

    //当前语音ID
    public currentVoiceId:string;
    // 当前播放语音ID
    public voiceId:string;

    // 语音播放停止切换
    private switchPlayOrStop:boolean=true;
    // 云端语音资源下载时间计时
    private downloadTimerLength:number=0;
    // 云端语音资源下载时间计时器
    private voiceDownloadTimer:any;
    // 语音播放计时
    private playTimerLength:number=null;
    // 语音播放计时器
    private playerVoiceTimer:any;

    constructor(
        private platform:Platform,
        private fileService:FileService,
        private recorderService:RecorderService,
        private toastService:ToastService
    ) {
        this.platform.pause.subscribe(()=>{
            this.stopVoice();
            this.switchPlayOrStop = true;
        })
    }

    ngOnDestroy(){
        this.stopVoice();
    }

    ngOnChanges(){
        this.stopVoice();
    }

    // 本地音频播放
    localPlayOrStop(name,length){
        if (!this.platform.is('cordova')) {
            return;
        }
        this.playTimerLength=null;
        const url=this.recorderService.getFilePath(name);
        this.recorderService.releasePlay();
        this.recorderService.startPlay(url);
        this.playTimerLength=length;
        if(this.playerVoiceTimer){
            clearInterval(this.playerVoiceTimer);
        }
        this.calcTimer();
    }

    // 播放或停止录音
    playOrStop(e,name,id,length) {
        e.stopPropagation();
        e.preventDefault();
        if (!this.platform.is('cordova')) {
            return;
        }
        if(!this.switchPlayOrStop){
            this.stopVoice();
            this.switchPlayOrStop=true;
            return false;
        }else{
            this.switchPlayOrStop=false;
        }
        if(this.isNewVoice){
            this.voiceId=id;
            this.localPlayOrStop(name,length);
            return false;
        }

        this.currentVoiceId=id;
        this.playTimerLength=null;
        this.voiceId=null;
        // 清除下载语音计时器
        if(this.voiceDownloadTimer){clearInterval(this.voiceDownloadTimer)}
        this.downloadTimerLength=0;
        this.calcDownloadTimer();
        // 下载语音
        this.fileService.getFile(name,name).subscribe((res)=>{
            this.recorderService.releasePlay();
            this.recorderService.startPlay(this.recorderService.getPlatformFileUrl(res.fileUrl));
            // 清除下载语音计时器
            clearInterval(this.voiceDownloadTimer);
            this.downloadTimerLength=0;
            // 播放计时器切换
            this.playTimerLength=length;
            this.voiceId=id;
            if(this.playerVoiceTimer){
                clearInterval(this.playerVoiceTimer);
            }
            this.calcTimer();
        },error=>{
            this.toastService.showToast('语音下载失败');
            clearInterval(this.voiceDownloadTimer);
        });
    }

    // 下载计时器
    calcDownloadTimer(){
        this.downloadTimerLength=0;
        this.voiceDownloadTimer=setInterval(()=>{
            this.downloadTimerLength++;
        },1000)
    }

    // 播放计时器
    calcTimer(){
        this.playerVoiceTimer=setInterval(()=>{
            this.playTimerLength--;
            if(this.playTimerLength<=0){
                if(!this.switchPlayOrStop){this.switchPlayOrStop = true;}
                clearInterval(this.playerVoiceTimer);
                this.playTimerLength=null;
                this.voiceId=null;
            }
        },1000)
    }

    // 停止播放音频
    stopVoice(){
        clearInterval(this.playerVoiceTimer);
        this.recorderService.stopPlay();
        this.recorderService.pausePlay();
        this.recorderService.releasePlay();
        this.voiceId=null;
    }
}
